package com.A21.Adapter;

import java.nio.charset.Charset;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList; 
import java.util.Date;
import java.util.Locale;

import android.app.Activity;
import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
 
import com.A21.Object.FrequentPurchaseList;  
import com.A21.Object.MSTProduct;
import com.A21MobileSales.R;

public class FrequentListAdapter extends BaseAdapter {
	ViewHolder holder;
	LayoutInflater inflater;
	ArrayList<FrequentPurchaseList> lstProducts; 
	 ArrayList<FrequentPurchaseList> mOriginalValues;
	static final String KEY_Product = "Product";
	static final String KEY_PName = "ProductName";
	private Context context;

	public FrequentListAdapter() {
		// TODO Auto-generated constructor stub
	}

	public FrequentListAdapter(Context _context,
			ArrayList<FrequentPurchaseList> _lstProducts) {
		Activity act = (Activity) _context;
		this.context = _context;
		this.lstProducts = _lstProducts;  
		this.mOriginalValues =new ArrayList<FrequentPurchaseList>();
		this.mOriginalValues.addAll(lstProducts);
		inflater = (LayoutInflater) act
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	public int getCount() {
		return lstProducts.size();
	}

	public Object getItem(int arg0) {
		return lstProducts.get(arg0);
	}

	public long getItemId(int arg0) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View vi = convertView;
		if (convertView == null) {
			vi = inflater.inflate(R.layout.row_product_selection, null);
			holder = new ViewHolder();
			holder.txtProduct = (TextView) vi
					.findViewById(R.id.PSLI_txtProduct);
			holder.txtPName = (TextView) vi.findViewById(R.id.PSLI_txtPName);
			vi.setTag(holder);
		} else {
			holder = (ViewHolder) vi.getTag();
		}
		try {
			//String strEncoded="";
			if(lstProducts.get(position).PName==null){ 
				lstProducts.get(position).PName="";
			}  
			//strEncoded=new String(lstProducts.get(position).PName.getBytes(Charset.forName("iso-8859-1")), Charset.forName("UTF-8"));  
			//String line1=  "<small><b>"+ String.format("%-30s", lstProducts.get(position).Product)  + "</b> <br/>" + strEncoded +"</small>"  ; 
			String line1=  "<small><b>"+ String.format("%-30s", lstProducts.get(position).Product)  + "</b> <br/>" + lstProducts.get(position).PName +"</small>"  ; 
			
			holder.txtProduct.setText(Html.fromHtml(line1));
			
			SimpleDateFormat dfs = new SimpleDateFormat("MMM dd, yyyy"); 
			Date DateDisp= null; 
			try {
				DateDisp=  dfs.parse(lstProducts.get(position).TrnDate);
			} catch (ParseException e) { 
				e.printStackTrace();
			}   
			final DateFormat dfDisp  = new SimpleDateFormat("dd-MM-yyyy"); 
			DecimalFormat df2 = new DecimalFormat("#0.00");
			String line2=  "Trn No. :"+ String.format("%-10s", lstProducts.get(position).TrnNo) + "  Date :" +  dfDisp.format(DateDisp)+ "  Price :" +  lstProducts.get(position).Currency +  df2.format(lstProducts.get(position).LastSellPrice);   
			holder.txtPName.setText(line2);
		} catch (Exception ex) {
			ex.printStackTrace();
		}

		return vi;
	}

	private class ViewHolder {
		TextView txtProduct;
		TextView txtPName;
	}

	
	
	public void filterByBMM(String brand, String model, String made,
			String charText) {
		// charText = charText.toLowerCase(Locale.getDefault());
		brand = brand.toLowerCase(Locale.getDefault());
		model = model.toLowerCase(Locale.getDefault());
		made = made.toLowerCase(Locale.getDefault());
		lstProducts.clear();
		if (brand.length() == 0 && model.length() == 0 && made.length() == 0
				&& charText.length() == 0) {
			lstProducts.addAll(mOriginalValues);
		} else {
			for (FrequentPurchaseList pc : mOriginalValues) {
				final String Brand = pc.Brand == null ? "" : pc.Brand;
				final String Model = pc.Model == null ? "" : pc.Model;
				final String Made = pc.Made == null ? "" : pc.Made;
				final String Product = pc.Product;
				final String ProductName = pc.PName; 
					if ((Product.toLowerCase().contains(charText) || ProductName.toLowerCase().contains(charText)) && Brand.toLowerCase().contains(brand)
							&& Model.toLowerCase().contains(model)
							&& Made.toLowerCase().contains(made)) {
						lstProducts.add(pc);
					}
				 
			}
		}
		notifyDataSetChanged();
	}
	
	
	 public void filter(String charText) {
			charText = charText.toLowerCase(Locale.getDefault());
			lstProducts.clear();
			if (charText.length() == 0) {
				lstProducts.addAll(mOriginalValues);
			} else {
				for (FrequentPurchaseList pc: mOriginalValues) {
					final String Product = pc.Product;
	                final String ProductName = pc.PName;
	                if (Product.toLowerCase().contains(charText.toString().toLowerCase()) || 
	                		ProductName.toLowerCase().contains(charText.toString().toLowerCase())){ 
	                	lstProducts.add(pc);
	                }
				}
			}
			notifyDataSetChanged();
		}
}